//
//  OTAddGroupVC.m
//  OT_OC
//
//  Created by yunmian on 2019/7/16.
//  Copyright © 2019 James. All rights reserved.
//

#import "OTAddGroupVC.h"

@interface OTAddGroupVC ()<UITextFieldDelegate>
{
    UIView                  *backgroundView;
    UITextField             *textField;
//    UIButton                *button;
    UIButton                *leftButton;
    UIButton                *rightButton;
    
    UIView                  *contentLine;
    UIView                  *buttonLine;
}
@end

@implementation OTAddGroupVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initUI];
}

- (void)initUI {
    
    self.view.backgroundColor = WhiteColor;
    
    backgroundView = [[UIView alloc] init];
    backgroundView.backgroundColor = WhiteColor;
    backgroundView.layer.cornerRadius = 2;
    backgroundView.clipsToBounds = YES;
    [self.view addSubview:backgroundView];
    
    leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftButton setTitleColor:ColorHex999999 forState:UIControlStateNormal];
    if (self.type == OTAddGroupVCTypeDate) {
        [leftButton setTitle:@"删除" forState:UIControlStateNormal];
    }else {
        [leftButton setTitle:@"取消" forState:UIControlStateNormal];
    }
    [leftButton addTarget:self action:@selector(onClickLeftButton) forControlEvents:UIControlEventTouchUpInside];
    [backgroundView addSubview:leftButton];
    
    rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightButton setTitleColor:ColorHexFC3D42 forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(onClickRightButton) forControlEvents:UIControlEventTouchUpInside];
    [rightButton setTitle:@"确定" forState:UIControlStateNormal];
    [backgroundView addSubview:rightButton];
    
    textField = [[UITextField alloc] init];
    textField.borderStyle = UITextBorderStyleLine;
    textField.delegate = self;
    textField.text = self.groupName;
    [backgroundView addSubview:textField];
    
    
    switch (self.type) {
        case OTAddGroupVCTypeDefault:
            textField.keyboardType = UIKeyboardTypeDefault;
            break;
        case OTAddGroupVCTypeNumber:
            textField.keyboardType = UIKeyboardTypeNumberPad;
            break;
        case OTAddGroupVCTypeDate:
            textField.keyboardType = UIKeyboardTypeNumberPad;
            break;
        case OTAddGroupVCTypeGroup:
            textField.keyboardType = UIKeyboardTypeDefault;
            break;
    }
    
    contentLine = [[UIView alloc] init];
    contentLine.backgroundColor = ColorHexF7F7F7;
    [backgroundView addSubview:contentLine];
    
    buttonLine = [[UIView alloc] init];
    buttonLine.backgroundColor = ColorHexF7F7F7;
    [rightButton addSubview:buttonLine];
    
//    button = [UIButton buttonWithType:UIButtonTypeCustom];
//    button.backgroundColor = UIColor.redColor;
//    button.layer.cornerRadius = 6;
//    [button setTitle:@"确定" forState:UIControlStateNormal];
//    [button addTarget:self action:@selector(onClickButton) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:button];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    [backgroundView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    [textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.offset(20);
        make.right.offset(-20);
        make.height.offset(30);
    }];
    
    [leftButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.equalTo(self->backgroundView);
        make.height.offset(40);
        make.width.offset(self.view.width / 2);
    }];
    
    [rightButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.equalTo(self->backgroundView);
        make.height.offset(40);
        make.width.offset(self.view.width / 2);
    }];
    
    [buttonLine mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.equalTo(self->rightButton);
        make.width.offset(1);
    }];
    
    [contentLine mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self->backgroundView);
        make.bottom.equalTo(self->leftButton.mas_top);
        make.height.offset(1);
    }];
}

#pragma mark Set
- (void)setGroupName:(NSString *)groupName {
    _groupName = groupName;
    textField.text = groupName;
}

- (void)setType:(OTAddGroupVCType)type {
    _type = type;
    if (type == OTAddGroupVCTypeDate) {
        [leftButton setTitle:@"删除" forState:UIControlStateNormal];
    }else {
        [leftButton setTitle:@"取消" forState:UIControlStateNormal];
    }
    
}

#pragma mark Event
- (void)onClickRightButton {
    
    [self zw_toLastViewController];
    
    if (self.type == OTAddGroupVCTypeGroup) {
        if ([self.delegate respondsToSelector:@selector(confirmEditGroupWithText:indexPath:)]) {
            [self.delegate confirmEditGroupWithText:textField.text indexPath:self.indexPath];
        }
    }else if (self.type == OTAddGroupVCTypeDate) {
        if ([self.delegate respondsToSelector:@selector(confirmEditDateWithOTTime:selectedDate:indexPath:)]) {
            [self.delegate confirmEditDateWithOTTime:textField.text selectedDate:self.selectDate indexPath:self.indexPath];
        }
    }else {
        if ([self.delegate respondsToSelector:@selector(confirmGroupWithText:)]) {
            [self.delegate confirmGroupWithText:textField.text];
        }
    }
}

- (void)onClickLeftButton {
    [self zw_toLastViewController];
    
    if (self.type == OTAddGroupVCTypeDate) {
        if ([self.delegate respondsToSelector:@selector(deleteEditDateWithIndexPath:)]) {
            [self.delegate deleteEditDateWithIndexPath:self.indexPath];
        }
    }else {
        if ([self.delegate respondsToSelector:@selector(cancelEditDateWithIndexPath:)]) {
            [self.delegate cancelEditDateWithIndexPath:self.indexPath];
        }
    }
}
@end
